import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 图形绘制器主窗口类
 */
public class ShapeDrawer extends JFrame {

    // 定义绘图面板
    private DrawArea drawArea = new DrawArea();

    /**
     * 构造函数：初始化图形界面
     */
    public ShapeDrawer() {
        setTitle("图形绘制器");                // 设置窗口标题
        setSize(400, 400);                   // 设置窗口大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 关闭时退出程序
        setLayout(new BorderLayout());       // 使用边界布局

        // 创建图形选择下拉框
        JComboBox<String> shapeSelector = new JComboBox<>(new String[]{
                "正方形", "长方形", "圆形", "三角形"
        });

        // 添加组件到窗口
        add(shapeSelector, BorderLayout.NORTH); // 下拉框放在顶部
        add(drawArea, BorderLayout.CENTER);     // 绘图画布放在中间

        // 为下拉框添加事件监听器
        shapeSelector.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox<?> cb = (JComboBox<?>) e.getSource();
                String shapeName = (String) cb.getSelectedItem();
                int size = 0; // 初始化尺寸变量
                boolean validInput = false; // 标记用户输入是否有效

                while (!validInput) { // 循环直到用户输入有效
                    String input = JOptionPane.showInputDialog("请输入" + shapeName + "的尺寸:");
                    try {
                        if (input == null) {
                            return; // 用户取消输入，直接返回
                        }
                        size = Integer.parseInt(input); // 尝试将输入转换为整数
                        if (size <= 0) {
                            JOptionPane.showMessageDialog(null, "输入无效，请输入一个正整数。");
                            continue;
                        }
                        validInput = true; // 如果成功转换，标记输入有效并退出循环
                    } catch (NumberFormatException ex) {
                        JOptionPane.showMessageDialog(null, "输入无效，请输入一个整数。"); // 如果失败，提示用户重新输入
                    }
                }
                drawArea.setShape(shapeName, size); // 更新图形和尺寸
            }
        });
    }

    /**
     * 程序入口点
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // 修正了方法名
            } catch (Exception ignored) {}
            new ShapeDrawer().setVisible(true);
        });
    }

    /**
     * 自定义绘图画布类
     */
    private static class DrawArea extends JPanel {
        private String shape = "正方形"; // 图形类型属性
        private int size = 100; // 添加尺寸属性，默认为100

        /**
         * 设置当前要绘制的图形和尺寸并触发重绘
         *
         * @param shape 图形名称
         * @param size  图形尺寸
         */
        public void setShape(String shape, int size) {
            this.shape = shape;
            this.size = size;
            this.repaint(); // 请求重新绘制面板
        }

        /**
         * 覆盖此方法实现自定义绘图逻辑
         */
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            int x = 50;
            int y = 50;

            switch (shape) {
                case "正方形":
                    g2d.drawRect(x, y, size, size);
                    break;
                case "长方形":
                    g2d.drawRect(x, y, size * 2, size);
                    break;
                case "圆形":
                    g2d.drawOval(x, y, size, size);
                    break;
                case "三角形":
                    int[] xPoints = {x + size / 2, x, x + size};
                    int[] yPoints = {y, y + size, y + size};
                    g2d.drawPolygon(xPoints, yPoints, 3);
                    break;
                default:
                    g2d.drawString("未知图形", 50, 50);
                    break;
            }
        }
    }
}
